package top.mschen.blog.controller.test;

import java.util.LinkedList;

/**
 * while和if
 */
public class Example10 {
  
    private LinkedList<Object> myList = new LinkedList<Object>();
      
    private int MAX = 10;  
      
    public Example10() {  
        super();  
    }  
  
    public void start(){  
        new Producerder().start();  
        new Consumereder().start();  
    }  
  
  
  
    class Producerder extends Thread {  
          
        @Override  
        public void run() {  
            for (int i = 0; i < 15; i++) {  
                try {  
                    this.sleep(1000);  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
                synchronized (myList) {  
                    try {  
                        while(myList.size() == MAX){  
                            System.out.println("warning: it's full!");  
                            myList.wait();  
                        }  
                          
                        //*************把这一段注释掉********************  
//                        Object o = new Object();
//                        if(myList.add(o)){
//                            System.out.println("Producer: " + o);
//                            myList.notify();
//                        }
                        //*******************************  
                        //把上面注释掉后,启用下面一行后。你再在Consumereder中分别是用while和if看下输出结果  
                        myList.notify();
                    } catch (Exception e) {  
                        System.out.println("producer is interrupted!");  
                    }  
                }  
            }  
        }  
          
    }  
      
      
    class Consumereder extends Thread {  
          
        @Override  
        public void run() {  
            for (int i = 0; i < 15; i++) {  
                try {  
                    this.sleep(100);  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
                  
                synchronized (myList) {  
                    try {  
                        if(myList.size() == 0){
                            System.out.println("warning: it's empty!");  
                            myList.wait();  
                        }  
                        System.out.println("oooooooooooooooooo");  
                        Object o = myList.removeLast();  
                        System.out.println("Consumereder" + o);  
                        myList.notify();  
                    } catch (Exception e) {  
                           System.out.println("consumer is interrupted!"+e.getMessage());
                    }  
                }  
            }  
        }  
          
    }  
  
      
    public static void main(String[] args) {

        int totalCount = 1;
        int totalPage = (totalCount % 10 == 0) ?
                totalCount / 10 :
                (totalCount / 10 + 1);
        System.out.println(totalPage);
//        Example10 ex10 = new Example10();
//        ex10.start();
    }  
} 